import 'package:flutter/material.dart';
import 'package:aweb5/base/BaseWidget.dart';
import 'package:aweb5/util/WindowUtil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:aweb5/util/HttpUtils1.dart';
import 'package:aweb5/bean/homepagebean_entity.dart';
import 'package:aweb5/util/HttpUtil.dart';
import 'package:aweb5/util/HttpClient.dart';
import 'package:aweb5/config/Config.dart';
import 'package:aweb5/bean/cate_entity.dart';
import 'package:aweb5/bean/product_detail_entity.dart';
import 'package:aweb5/bean/order_list_entity.dart';
import 'package:aweb5/bean/login_entity.dart';

class HomeTab extends BaseWidget {
  @override
  State<HomeTab> createState() => HomeTabState();
}

class HomeTabState extends BaseWidgetState<HomeTab> {
  List<String> image = List();

  @override
  void initState() {
    super.initState();
    image.add("assets/banner1.png");
    image.add("assets/pic.jpg");
    request5();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: Text(
          "优鲜百科",
          style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold),
        ),
        centerTitle: true,
        elevation: 0,
      ),
      body: Stack(
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(top: 44),
            child: ListView(
              children: <Widget>[banner(), recommand(), rec(), grid()],
            ),
          ),
          Positioned(
            child: locateAndSearch(),
            top: 0,
          ),
        ],
      ),
    );
  }

  Widget locateAndSearch() {
    return Container(
      margin: EdgeInsets.only(right: 10),
      color: Color(0xfffafafa),
      width: WindowUtil.window_width,
      height: 44,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: <Widget>[
          SizedBox(
            width: 10,
          ),
          Image.asset(
            "assets/location.png",
            width: 15,
            height: 17,
          ),
          Container(
            width: 120.0,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Container(
                  width: 87,
                  child: Text(
                    " 桂林路牛奶定点",
                    overflow: TextOverflow.ellipsis,
                    maxLines: 1,
                    style: TextStyle(color: Color(0xffff8900), fontSize: 12),
                  ),
                ),
                Image.asset(
                  "assets/tri.png",
                  width: 8,
                  height: 5,
                )
              ],
            ),
          ),
          Expanded(
              child: Container(
            height: 30,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(15),
                color: Color(0xffeeeeee)),
            child: TextField(
              decoration: InputDecoration(
                  hintText: "请输入商品名称",
                  hintStyle: TextStyle(
                    color: Color(0xffa1a1a1),
                    fontSize: 12,
                  ),
                  border: InputBorder.none,
                  contentPadding: EdgeInsets.only(top: 5.0),
                  prefixIcon: Container(
                    width: 1,
                    height: 1,
                    padding: const EdgeInsets.fromLTRB(15.0, 0, 15.0, 0),
                    child: Center(child: Image.asset("assets/sousuo_icon.png")),
                  )),
            ),
          ))
        ],
      ),
    );
  }

  Widget banner() {
    return Container(
      height: 230.0,
      child: new Swiper(
        itemBuilder: (BuildContext context, int index) {
          return new Image.asset(image[index],
              fit: BoxFit.fill,
              height: MediaQuery.of(context).size.height * 0.2);
        },
        itemCount: image.length,
        autoplay: true,
        onIndexChanged: (int s) {
//               setState(() {});
        },
        pagination:
////          new SwiperPagination(builder: SwiperPagination.fraction),
            new SwiperPagination(
                builder: DotSwiperPaginationBuilder(
          color: Colors.black54,
          activeColor: Colors.white,
        )),
      ),
    );
  }

  Widget recommand() {
    return Container(
      margin: EdgeInsets.all(13),
      child: Image.asset("assets/youhui.png"),
    );
  }

  Widget rec() {
    return Container(
      width: WindowUtil.window_width,
      margin: EdgeInsets.only(top: 5),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Image.asset(
            "assets/group.png",
            width: 32,
            height: 15,
          ),
          SizedBox(
            width: 10,
          ),
          Text(
            "推荐商品",
            style: TextStyle(color: Colors.black, fontSize: 13),
          ),
          SizedBox(
            width: 10,
          ),
          Image.asset(
            "assets/group2.png",
            width: 32,
            height: 15,
          ),
        ],
      ),
    );
  }

  Widget grid() {
    double a = ((WindowUtil.window_width - 10) / 2) /
        (20 + (WindowUtil.window_width - 10 - 20) / 2 + 10 + 15 + 40 + 40);
    print(">>${a}");
    return GridView.count(
      crossAxisCount: 2,
      crossAxisSpacing: 5,
      mainAxisSpacing: 5,
      children: getWidgetList(),
      childAspectRatio: a,
      shrinkWrap: true,
      physics: NeverScrollableScrollPhysics(),
    );
//    ((WindowUtil.window_width-30)/2-20)/(20+(WindowUtil.window_width-30)/2+10+15+40+40)
  }

  List<String> getDataList() {
    List<String> list = [];
    for (int i = 0; i < 100; i++) {
      list.add(i.toString());
    }
    return list;
  }

  List<Widget> getWidgetList() {
    return getDataList().map((item) => gridItem(item)).toList();
  }

  Widget getItemContainer(String item) {
    return Container(
      alignment: Alignment.center,
      child: Text(
        item,
        style: TextStyle(color: Colors.white, fontSize: 20),
      ),
      color: Colors.blue,
    );
  }

  Widget gridItem(String item) {
    return Card(
      child: Container(
        child: Column(
          children: <Widget>[
            Image.asset(
              "assets/bitmap.png",
              width: ((WindowUtil.window_width - 30) / 2 - 20),
              height: ((WindowUtil.window_width - 30) / 2 - 20),
              fit: BoxFit.fill,
            ),
            SizedBox(
              height: 10,
            ),
            Container(
              height: 40.0,
              child: Text(
                "早餐牛奶面包牛奶牛奶牛奶牛奶牛奶",
                maxLines: 2,
                style: TextStyle(color: Color(0xff191919), fontSize: 12),
              ),
            ),
            SizedBox(
              height: 15,
            ),
            Container(
              height: 40,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Column(
                    children: <Widget>[
                      Text(
                        "¥30",
                        style:
                            TextStyle(color: Color(0xffff0000), fontSize: 16),
                      ),
                      Text(
                        "会员价",
                        style:
                            TextStyle(color: Color(0xffff8600), fontSize: 12),
                      )
                    ],
                  ),
                  Image.asset(
                    "assets/gouwucheselect.png",
                    width: 20,
                    height: 20,
                  )
                ],
              ),
            )
          ],
        ),

        // width: (WindowUtil.window_width-30)/2,

        padding: EdgeInsets.all(10),
      ),
    );
  }

  void request() async {
    try {
      Map<String,dynamic> map=Map();
      map["t"]="1568972414822";
      map["k"]="57b4e4cdb289a073a340e9ed7b5044fa";
      map["customer_id"]="2";
      map["uuid"]="816604fa-f57d-b47c-b32b-3fe12f0c4d85";
      map["platform"]="weishop";
      map["device"]="micromessenger";
      map["customer_id"]="2";
      Map<String ,dynamic> map1 = await  HttpClient.requestHome(Config.baseUrl+"HomePagePreview",data:map);
      HomepagebeanEntity homepagebeanEntity = HomepagebeanEntity.fromJson(map1);
    //  t=1568972414822&k=57b4e4cdb289a073a340e9ed7b5044fa&customer_id=2&uuid=816604fa-f57d-b47c-b32b-3fe12f0c4d85&platform=weishop&device=micromessenger&v=2&customer_id=2
      if(homepagebeanEntity==null){
        print("null");
      }else{
        print("not null");
      }
    } catch (e) {}
       print("err  ");
    // HttpUtil.get(baseUrl+"HomePagePreview?t=1568972414822&k=57b4e4cdb289a073a340e9ed7b5044fa&customer_id=2&uuid=816604fa-f57d-b47c-b32b-3fe12f0c4d85&platform=weishop&device=micromessenger&v=2&customer_id=2");
  }
  void request1() async {
    try {
    //  http://hhnrest2yxbk.test.freshqiao.com/CategoryList?t=1569382730805&k=6d962d4cc78a4517b48fe658d79245bf&customer_id=2&uuid=1bb9340c-3d89-cb54-997a-b8ea301a32ff&platform=weishop&device=micromessenger&v=2
      Map<String,dynamic> map=Map();
      map["t"]="1569382730805";
      map["k"]="6d962d4cc78a4517b48fe658d79245bf";
      map["customer_id"]="2";
      map["uuid"]="1bb9340c-3d89-cb54-997a-b8ea301a32ff";
      map["platform"]="weishop";
      map["device"]="micromessenger";
      map["customer_id"]="2";
      map["v"]="2";
    //  Map<String ,dynamic> map1 = await  HttpClient.requestHome(Config.baseUrl+"CategoryList",data:map);
      Map<String ,dynamic> map1 = await  HttpClient.requestHome(Config.baseUrl+"CategoryList",data:map);
      CateEntity cateEntity = CateEntity.fromJson(map1);
    //  t=1568972414822&k=57b4e4cdb289a073a340e9ed7b5044fa&customer_id=2&uuid=816604fa-f57d-b47c-b32b-3fe12f0c4d85&platform=weishop&device=micromessenger&v=2&customer_id=2
      if(cateEntity==null){
        print("null");
      }else{
        print("not null");
      }
    } catch (e) {}
       print("err  ");
    // HttpUtil.get(baseUrl+"HomePagePreview?t=1568972414822&k=57b4e4cdb289a073a340e9ed7b5044fa&customer_id=2&uuid=816604fa-f57d-b47c-b32b-3fe12f0c4d85&platform=weishop&device=micromessenger&v=2&customer_id=2");
  }

  /**
   * 商品详情
   */
  void request2() async {
    try {
     // http://hhnrest2yxbk.test.freshqiao.com/Product/1?t=1569395229106&k=4660d11402620c44d2e92e0910808c29&customer_id=2&uuid=b145a130-1d98-d32c-f1c5-bf8624368608&platform=weishop&device=micromessenger&v=2
       Map<String,dynamic> map=Map();
      map["t"]="1569382730805";
      map["k"]="6d962d4cc78a4517b48fe658d79245bf";
      map["customer_id"]="2";
      map["uuid"]="1bb9340c-3d89-cb54-997a-b8ea301a32ff";
      map["platform"]="weishop";
      map["device"]="micromessenger";

      map["v"]="2";
    //  Map<String ,dynamic> map1 = await  HttpClient.requestHome(Config.baseUrl+"CategoryList",data:map);
      Map<String ,dynamic> map1 = await  HttpClient.requestProductDetail(Config.baseUrl+'Product/23',data:map);
     // Map<String ,dynamic> map1 = await  HttpClient.requestProductDetail('http://hhnrest2yxbk.test.freshqiao.com/Product/1?t=1569395229106&k=4660d11402620c44d2e92e0910808c29&customer_id=2&uuid=b145a130-1d98-d32c-f1c5-bf8624368608&platform=weishop&device=micromessenger&v=2');
      CateEntity cateEntity = CateEntity.fromJson(map1);
    //  t=1568972414822&k=57b4e4cdb289a073a340e9ed7b5044fa&customer_id=2&uuid=816604fa-f57d-b47c-b32b-3fe12f0c4d85&platform=weishop&device=micromessenger&v=2&customer_id=2
      if(cateEntity==null){
        print("null");
      }else{
        print("not null");
      }
    } catch (e) {}
       print("err  ");
    // HttpUtil.get(baseUrl+"HomePagePreview?t=1568972414822&k=57b4e4cdb289a073a340e9ed7b5044fa&customer_id=2&uuid=816604fa-f57d-b47c-b32b-3fe12f0c4d85&platform=weishop&device=micromessenger&v=2&customer_id=2");
  }

  /**
   * 商品推荐列表
   */
  void request3() async {
    try {
    //  http://hhnrest2yxbk.test.freshqiao.com/CategoryRecommendProductList?t=1569465315921&k=5657a97b0e614cf6d5337387a2b0426b&customer_id=2&uuid=a8dd907b-d0da-583f-edfd-dca1d279e14d&platform=weishop&device=micromessenger&v=2&category_id=7
      Map<String,dynamic> map=Map();
      map["t"]="1569382730805";
      map["k"]="6d962d4cc78a4517b48fe658d79245bf";
      map["customer_id"]="2";
      map["uuid"]="1bb9340c-3d89-cb54-997a-b8ea301a32ff";
      map["platform"]="weishop";
      map["device"]="micromessenger";

      map["v"]="2";
      map["category_id"]="7";
    //  Map<String ,dynamic> map1 = await  HttpClient.requestHome(Config.baseUrl+"CategoryList",data:map);
      Map<String ,dynamic> map1 = await  HttpClient.requestProductList(Config.baseUrl+'CategoryRecommendProductList',data:map);
     // Map<String ,dynamic> map1 = await  HttpClient.requestProductDetail('http://hhnrest2yxbk.test.freshqiao.com/Product/1?t=1569395229106&k=4660d11402620c44d2e92e0910808c29&customer_id=2&uuid=b145a130-1d98-d32c-f1c5-bf8624368608&platform=weishop&device=micromessenger&v=2');
      ProductDetailEntity productDetailEntity = ProductDetailEntity.fromJson(map1);
    //  t=1568972414822&k=57b4e4cdb289a073a340e9ed7b5044fa&customer_id=2&uuid=816604fa-f57d-b47c-b32b-3fe12f0c4d85&platform=weishop&device=micromessenger&v=2&customer_id=2
      if(productDetailEntity==null){
        print("null");
      }else{
        print("not null");
      }
    } catch (e) {}
       print("err  ");
    // HttpUtil.get(baseUrl+"HomePagePreview?t=1568972414822&k=57b4e4cdb289a073a340e9ed7b5044fa&customer_id=2&uuid=816604fa-f57d-b47c-b32b-3fe12f0c4d85&platform=weishop&device=micromessenger&v=2&customer_id=2");
  }

  /**
   * 订单
   */
  void request4() async {
    try {

     // http://hhnrest2yxbk.test.freshqiao.com/OrderList?t=1569484173259&k=39ff987ae5424f42d0356167eaa384b5&customer_id=2&uuid=949838ce-2727-4a0c-0749-b5e006657247&platform=weishop&device=micromessenger&v=2&client_id=9&status=1,2,3,4,5,6,7&l=5&o=0
      Map<String,dynamic> map=Map();
      map["t"]="1569382730805";
      map["k"]="6d962d4cc78a4517b48fe658d79245bf";
      map["customer_id"]="2";
      map["uuid"]="1bb9340c-3d89-cb54-997a-b8ea301a32ff";
      map["platform"]="weishop";
      map["device"]="micromessenger";
      map["client_id"]='9';
      map["status"]="1,2,3,4,5,6,7";
      map["l"]="1";
      map["o"]="0";
    //  Map<String ,dynamic> map1 = await  HttpClient.requestHome(Config.baseUrl+"CategoryList",data:map);
      Map<String ,dynamic> map1 = await  HttpClient.requestOrderList(Config.baseUrl+'OrderList',data:map);
     // Map<String ,dynamic> map1 = await  HttpClient.requestProductDetail('http://hhnrest2yxbk.test.freshqiao.com/Product/1?t=1569395229106&k=4660d11402620c44d2e92e0910808c29&customer_id=2&uuid=b145a130-1d98-d32c-f1c5-bf8624368608&platform=weishop&device=micromessenger&v=2');
      OrderListEntity orderListEntity = OrderListEntity.fromJson(map1);
    //  t=1568972414822&k=57b4e4cdb289a073a340e9ed7b5044fa&customer_id=2&uuid=816604fa-f57d-b47c-b32b-3fe12f0c4d85&platform=weishop&device=micromessenger&v=2&customer_id=2
      if(orderListEntity==null){
        print("null");
      }else{
        print("not null");
      }
    } catch (e) {}
       print("err  ");
    // HttpUtil.get(baseUrl+"HomePagePreview?t=1568972414822&k=57b4e4cdb289a073a340e9ed7b5044fa&customer_id=2&uuid=816604fa-f57d-b47c-b32b-3fe12f0c4d85&platform=weishop&device=micromessenger&v=2&customer_id=2");
  }

  /**
   * 登录
   */
  void request5() async {
    try {
     // http://hhnrest2yxbk.test.freshqiao.com/Register?t=1569484772499&k=7cf9d33a59881ff5a532ddfd0f7df425&customer_id=2&uuid=20453f63-a73a-1193-6c4c-64462d2875dd&platform=weishop&device=micromessenger&v=2&mobile=18221256342
     // http://hhnrest2yxbk.test.freshqiao.com/OrderList?t=1569484173259&k=39ff987ae5424f42d0356167eaa384b5&customer_id=2&uuid=949838ce-2727-4a0c-0749-b5e006657247&platform=weishop&device=micromessenger&v=2&client_id=9&status=1,2,3,4,5,6,7&l=5&o=0
      Map<String,dynamic> map=Map();
      map["t"]="1569382730805";
      map["k"]="6d962d4cc78a4517b48fe658d79245bf";
      map["customer_id"]="2";
      map["uuid"]="1bb9340c-3d89-cb54-997a-b8ea301a32ff";
      map["platform"]="weishop";
      map["device"]="micromessenger";
     // map["client_id"]='9';
      map["v"]="2";

      map["mobile"]="18221256342";

    //  Map<String ,dynamic> map1 = await  HttpClient.requestHome(Config.baseUrl+"CategoryList",data:map);
      Map<String ,dynamic> map1 = await  HttpClient.requestLogin(Config.baseUrl+'Register',data:map);
     // Map<String ,dynamic> map1 = await  HttpClient.requestProductDetail('http://hhnrest2yxbk.test.freshqiao.com/Product/1?t=1569395229106&k=4660d11402620c44d2e92e0910808c29&customer_id=2&uuid=b145a130-1d98-d32c-f1c5-bf8624368608&platform=weishop&device=micromessenger&v=2');
      LoginEntity loginEntity = LoginEntity.fromJson(map1);
    //  t=1568972414822&k=57b4e4cdb289a073a340e9ed7b5044fa&customer_id=2&uuid=816604fa-f57d-b47c-b32b-3fe12f0c4d85&platform=weishop&device=micromessenger&v=2&customer_id=2
      if(loginEntity==null){
        print("null");
      }else{
        print("not null");
      }
    } catch (e) {}
       print("err  ");
    // HttpUtil.get(baseUrl+"HomePagePreview?t=1568972414822&k=57b4e4cdb289a073a340e9ed7b5044fa&customer_id=2&uuid=816604fa-f57d-b47c-b32b-3fe12f0c4d85&platform=weishop&device=micromessenger&v=2&customer_id=2");
  }




}
